﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ServiceStack.Redis;

/// <summary>
/// KeaoRedis 的摘要说明
/// </summary>
public class KeaoRedis
{
    public static void WriteString(string key, string value)
    {
        using (IRedisClient redis = RedisClientPool.GetRedisClient())
        {
            if (redis != null)
            {
                redis.SetValue(key, value);
            }
        }
    }

    public static void WriteString(string key, string value, double expireSeconds)
    {
        using (IRedisClient redis = RedisClientPool.GetRedisClient())
        {
            if (redis != null)
            {
                redis.SetValue(key, value,TimeSpan.FromSeconds(expireSeconds));
            }
        }
    }

    public static string ReadString(string key)
    {
        using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
        {
            if (redis != null)
            {
                if (redis.ContainsKey(key))
                {
                    return redis.GetValue(key);
                }
            }
        }
        return string.Empty;
    }

    public static void RemoveByKey(string key)
    {
        using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
        {
            if (redis != null)
            {
                redis.Remove(key);
            }
        }
    }

    public static bool ContainsKey(string key)
    {
        using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
        {
            if (redis != null)
            {
                return redis.ContainsKey(key);
            }
            else
            {
                return false;
            }
        }
    }
}